package com.gui;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public class PasswrodBasedCrypto {

	// private static byte[] recoveredText=null;
	// private static byte[] ciphertext = null;
	private byte[] eText = null;
	private byte[] dText = null;
	private PBEParameterSpec pbeParamSpec;

	public PasswrodBasedCrypto() {
		// Salt
		byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
				(byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };

		// Iteration count
		int count = 20;

		// Create PBE parameter set
		pbeParamSpec = new PBEParameterSpec(salt, count);

		// byte[] eText = encrytoText(pbeParamSpec,pwd);
		// byte[] dText = decrytoText(pbeParamSpec, eText,confirmpwd);
	}

/*	private static byte[] encrytoText(PBEParameterSpec pbeParamSpec,
			char[] pwd, byte[] pvKeyEncoded) {

		PBEKeySpec pbeKeySpec;
		SecretKeyFactory keyFac;
		byte[] ciphertext = null;
		try {
			pbeKeySpec = new PBEKeySpec(pwd);
			keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

			// Create PBE Cipher
			Cipher encryptor = Cipher.getInstance("PBEWithMD5AndDES");
			Cipher decryptor = Cipher.getInstance("PBEWithMD5AndDES");

			// Initialize PBE Cipher with key and parameters
			encryptor.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
			decryptor.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

			// Our cleartext
			byte[] cleartext = pvKeyEncoded;
			System.out.println("OriginalText : " + pvKeyEncoded.toString());
			MainWindows.setTextLog("OriginalText : " + pvKeyEncoded.toString());
			// Encrypt the cleartext
			ciphertext = encryptor.doFinal(cleartext);
			String temp1 = new String(cleartext);
			System.out.println("원본 문자열 : " + temp1);
			MainWindows.setTextLog("원본 문자열 : " + temp1);
			String temp2 = new String(ciphertext);
			System.out.println("암호화 문자열 : " + temp2);
			MainWindows.setTextLog("암호화 문자열 : " + temp2);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ciphertext;
	}*/

	private static byte[] decrytoKey(PBEParameterSpec pbeParamSpec,
			byte[] ciphertext, char[] confirmpwd)
			throws IllegalBlockSizeException, NoSuchAlgorithmException,
			InvalidKeySpecException, NoSuchPaddingException,
			InvalidKeyException, InvalidAlgorithmParameterException {

		PBEKeySpec pbeKeySpec;
		SecretKeyFactory keyFac;
		byte[] recoveredText = null;
		try {
			pbeKeySpec = new PBEKeySpec(confirmpwd);
			keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

			// Create PBE Cipher
			Cipher decryptor = Cipher.getInstance("PBEWithMD5AndDES");

			// Initialize PBE Cipher with key and parameters
			decryptor.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

			int recoveredTextLen = decryptor.getOutputSize(ciphertext.length);
			recoveredText = new byte[recoveredTextLen];
			recoveredText = decryptor.doFinal(ciphertext);

			System.out.println("암호화 PVkey :: " + new String(ciphertext));
			MainWindows.setTextLog("암호화 PVkey :: " + new String(ciphertext));
			System.out.println("복호화 PVkey :: " + new String(recoveredText));
			MainWindows.setTextLog("복호화 PVkey :: " + new String(recoveredText));

		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			System.out.println("fail");
			MainWindows.setTextLog("fail");
			return null;
			// e.printStackTrace();
		}
		return recoveredText;
	}

//	public byte[] getEncryptionPVkey(char[] pwd, byte[] encodedKey) {
//		byte[] encryptPVkey = encrytoText(pbeParamSpec, pwd, encodedKey);
//		return encryptPVkey;
//	}

	public byte[] getDecryptionPVKey(char[] confirmpwd, byte[] enCryptText) throws InvalidKeyException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException {

		byte[] recoveredPVkey = decrytoKey(pbeParamSpec, enCryptText,
				confirmpwd);
		if (recoveredPVkey == null) {
			return null;
		} else {
			return recoveredPVkey;
		}
	}
}
